import json
import urllib.request


class _Weather(object):
    def get_weather(self):
        url = 'http://api.avatardata.cn/Weather/Query?key=ed189918daba41f09f5054496914c192&cityname=%E6%B2%88%E9%98%B3'
        data = urllib.request.urlopen(url).read()
        #改变编码集
        data=data.decode("utf-8")
        #将返回的json数据类型解析为python数据类型（object -> dict）
        data2 = json.loads(data)
        #print('data2',data2)
        #print(data2['result']['realtime']['weather']['info'])


        info = data2['result']['realtime']['weather']['info']
        img = data2['result']['realtime']['weather']['img']
        temperature = data2['result']['realtime']['weather']['temperature']
        c_a_mag = data2['result']['life']['info']['chuanyi'][1]
        c_mag = data2['result']['life']['info']['chuanyi'][0]
        kongtiao_1 = data2['result']['life']['info']['kongtiao'][1]
        feng_direct = data2['result']['realtime']["wind"]['direct']
        feng_power = data2['result']['realtime']["wind"]['power']
        ziwaixian_1 = data2['result']['life']['info']['ziwaixian'][1]
        yundong_1 = data2['result']['life']['info']['yundong'][1]
        weather_mg = {}
        weather_mg['info'] = info
        weather_mg['img'] = img
        weather_mg['temperature'] = temperature
        weather_mg['c_mag'] = c_mag
        weather_mg['c_a_mag'] = c_a_mag
        weather_mg['kongtiao_1'] = kongtiao_1
        weather_mg['feng_direct'] = feng_direct
        weather_mg['feng_power'] = feng_power
        weather_mg['ziwaixian_1'] = ziwaixian_1
        weather_mg['yundong_1'] = yundong_1



        print(data)


        return weather_mg

all = {"result":
                   {"realtime":
                        {"wind":
                             {
                                 "windspeed": "", "direct": "西北风", "power": "3级", "offset": ""
                              },
                         "time": "14:53:04","weather":
                             {
                                 "humidity": "30", "img": "00", "info": "晴", "temperature": "-5"
                             },
                         "dataUptime": "1511074384", "date": "2017-11-19", "city_code": "101070101", "city_name": "沈阳",
                         "week": "0", "moon": "十月初二"
                        },
                       "life":
                       {
                           "date": "2017-11-19", "info":
                           {
                               "kongtiao":
                                   ["开启制暖空调", "您将感到有些冷，可以适当开启制暖空调调节室内温度，以免着凉感冒。"],
                                "yundong":
                                    ["较不宜", "天气较好，但考虑天气寒冷，推荐您进行各种室内运动，若在户外运动请注意保暖并做好准备活动。"],
                                "ziwaixian":
                                    ["最弱", "属弱紫外线辐射天气，无需特别防护。若长期在户外，建议涂擦SPF在8-12之间的防晒护肤品。"],
                                "ganmao":
                                    ["较易发", "天气较凉，较易发生感冒，请适当增加衣服。体质较弱的朋友尤其应该注意防护。"],
                                "xiche":
                                    ["较适宜", "较适宜洗车，未来一天无雨，风力较小，擦洗一新的汽车至少能保持一天。"],
                                "wuran": "null",
                                "chuanyi":
                                    ["寒冷", "天气寒冷，建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。"]
                           }
                       },
                       "weather":
                       [
                           {
                               "date": "2017-11-19", "week": "日", "nongli": "十月初二","info":
                               {
                                   "dawn":
                                       ["0", "晴", "-13", "西北风", "微风", "16:24"],
                                    "day":
                                        ["0", "晴", "-2", "西北风", "微风", "06:39"],
                                    "night":
                                        ["1", "多云", "-7", "西南风", "微风", "16:23"]
                               }
                           },
                            {
                                "date": "2017-11-20", "week": "一", "nongli": "十月初三",
                                "info": {"dawn": ["1", "多云", "-7", "西南风", "微风", "16:23"], "day": ["0", "晴", "4", "西北风", "微风", "06:40"],
                                "night": ["0", "晴", "-5", "西南风", "微风", "16:23"]}
                            },
                            {
                                "date": "2017-11-21", "week": "二", "nongli": "十月初四",
                                "info": {"dawn": ["0", "晴", "-5", "西南风", "微风", "16:23"],
                                "day": ["1", "多云", "8", "西南风", "4-5级", "06:41"],
                                "night": ["1", "多云", "-4", "西南风", "3-5级", "16:22"]}
                            },
                            {
                                "date": "2017-11-22", "week": "三", "nongli": "十月初五",
                                    "info": {"dawn": ["1", "多云", "-4", "西南风", "3-5级", "16:22"],
                                      "day": ["0", "晴", "3", "西北风", "3-5级", "06:42"],
                                      "night": ["0", "晴", "-7", "西北风", "3-5级", "16:21"]}
                            },
                            {
                                "date": "2017-11-23", "week": "四", "nongli": "十月初六",
                                "info": {"dawn": ["0", "晴", "-7", "西北风", "3-5级", "16:21"],
                                      "day": ["0", "晴", "2", "西北风", "3-5级", "06:44"],
                                      "night": ["0", "晴", "-7", "西北风", "3-5级", "16:20"]}
                            }],
            "pm25": {"key": "Shenyang", "show_desc": "0",
                     "pm25": {"curPm": "29", "pm25": "14", "pm10": "25", "level": "1", "quality": "优",
                              "des": "空气很好，可以外出活动"}, "dateTime": "2017年11月19日14时", "cityName": "沈阳"}, "isForeign": 0},
         "error_code": 0, "reason": "Succes"}
if __name__ == "__main__":
    city = '沈阳'
    # a = u"中文"
    # a_gb2312 = a.encode('gb2312')
    # print(a_gb2312)
    #
    # a_unicode = a_gb2312.decode('gb2312')
    # assert (a_unicode == a)
    # a_utf_8 = a_unicode.encode('utf-8')
    # print(a_utf_8)
    _Weather.get_weather(city)